package com.example.graduatetest.view.model;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.graduatetest.R;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.databinding.ActivityAddClasssBinding;
import com.example.graduatetest.model.ClassModel;

public class AddClassViewModel extends ViewModel {
    private ActivityAddClasssBinding binding;
    private Activity activity;
    private Handler handler = new Handler();
    private MutableLiveData<String> idText;
    private MutableLiveData<String> changeText;
    private String toastText="加入课程失败啦！";
    private String studentNumber;
    public void init(Activity activity,ActivityAddClasssBinding binding){
        this.activity = activity;
        this.binding = binding;
        if(idText == null) idText = new MutableLiveData<>();
        if(changeText == null) changeText = new MutableLiveData<>();
        changeText.setValue("未输入课程号");
        studentNumber = getStudentNumber();
        toolbarInit();
        setClick();
    }

    private String getStudentNumber(){
        SharedPreferences preferences = activity.getSharedPreferences("data", Context.MODE_PRIVATE);
        return  preferences.getString("number", "2017211973");
    }

    //初始课程
    private void toolbarInit(){
        Toolbar toolbar = binding.toolbar3;
        toolbar.setTitle("加入课程");
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });

    }
    private void setClick(){
        binding.btAddClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(idText.getValue() == null){
                    Toast.makeText(activity, "请输入课程", Toast.LENGTH_SHORT).show();
                }else{
                    changeText.setValue("加入课程中......");
                    ClassModel.addClass(idText.getValue(), studentNumber, new StringCallBack() {
                        @Override
                        public void onSuccess(String result) {
                            if(result.equals("success")){
                                toastText = "加入课程成功啦！";

                            }else{
                                toastText = "没有该课程号";
                            }
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    changeText.setValue(toastText);
                                    if(toastText.equals("加入课程成功啦！")){
                                        activity.onBackPressed();
                                    }
                                }
                            });


                        }

                        @Override
                        public void onFailed() {
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    changeText.setValue("加入课程失败啦！");
                                }
                            });
                        }
                    });
                }
            }
        });
        binding.etAddClass.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                changeText.setValue("正在输入课程号");
            }
        });
    }

    public MutableLiveData<String> getChangeText() {
        return changeText;
    }

    public MutableLiveData<String> getIdText() {
        return idText;
    }
}
